package cn.fl.mcr.service.impl;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.web.core.framework.controller.BaseController;
import cn.fl.mcr.service.IMcrSideAgreementService;
import cn.fl.mcr.transform.McrSideAgreementTransform;
import cn.fl.mcr.vo.McrSideAgreementVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.base.constant.BaseConstant;
import cn.lionbridgecapital.loan.model.dto.McrSideAgreementDTO;
import cn.lionbridgecapital.loan.model.exception.BaseException;
import cn.lionbridgecapital.loan.model.utils.CollectionUtils;
import cn.lionbridgecapital.loan.model.utils.MoneyUtils;
import cn.lionbridgecapital.loan.model.utils.StringUtils;
import cn.lionbridgecapital.loan.service.api.contract.IMcrSideAgrContApiService;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.dto.CstBscInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author YangXin
 * @Description: 签署补充协议合同实体类
 * @date 2020/4/26 13:57
 */
@Service
public class McrSideAgreementServiceImpl implements IMcrSideAgreementService {

    @Autowired
    private IMcrSideAgrContApiService mcrSideAgrContApiService;
    @Autowired
    private ICustomerQueryApiService customerQueryApiService;

    @Override
    public Pager searchListPage(Pager pager) throws BaseException {
        Pager pager1 = this.mcrSideAgrContApiService.searchListPage(pager);
        List<McrSideAgreementDTO> mcrSideAgreementDTOList = (List<McrSideAgreementDTO>)pager1.getResultList();
        List<McrSideAgreementVO> mcrSideAgreementVOList = McrSideAgreementTransform.toVOList(mcrSideAgreementDTOList);

        if (CollectionUtils.isNotEmpty(mcrSideAgreementVOList)){
            for (McrSideAgreementVO vo : mcrSideAgreementVOList) {
                // 获取客户信息
                CstBscInfoDTO cstBscInfoDTO = this.customerQueryApiService.getCstBscInfoById(vo.getCstId());
                if (!Objects.isNull(cstBscInfoDTO)){
                    vo.setCstNm(cstBscInfoDTO.getCstNm());
                }
                if (StringUtils.isNotEmpty(vo.getLoanAmt())){
                    vo.setLoanAmtMoney(MoneyUtils.fenToYuan(vo.getLoanAmt()).doubleValue());
                }
            }
        }
        pager1.setResultList(mcrSideAgreementVOList);
        return pager1;
    }

    @Override
    public DataResultDTO signSideAgrContract(Long cstId, String contractNo) throws BaseException {
        return this.mcrSideAgrContApiService.signSideCompany(cstId,contractNo);
    }
}
